﻿#Include ..\_lib\Theme.ahk
#Include ..\_lib\Tip.ahk
#Include ..\_lib\TipUI.ahk
#Include ..\_lib\Animation.ahk
#Include ..\_lib\Receiver.ahk
#Include ..\_lib\log.ahk
#Include ..\_lib\Lerp.ahk


Receiver.OnCopyDataCB := CB

CB(data) {
  period := data.substring(1, i := InStr(data, '@'))
  detail := data.substring(i + 1)
  Tip.ShowTip('收到任务')
  SetTimer(Toast.Bind(detail, FormatTime(, 'HH:mm:ss'), period), period * 1e3 * -1)
}

Toast(str, date, period) {
  g := Gui('+AlwaysOnTop +ToolWindow -Caption +Border')
  g.SetFont('s14', 'consolas')
  g.AddText('Section', str).GetPos(, , &w)
  g.AddText('xs y34 Backgroundffd500 h1 w' w)
  g.SetFont('s13')
  g.AddText('xs y35', FormatTime(, date ' - HH:mm:ss -- ' period))
  g.OnEvent('ContextMenu', (*) => _checked := true)
  Theme.Dark(g)
  g.Show('Hide'), g.GetPos(, , &w, &h)
  p := g.AddText('x0 y' h - 4 ' Backgroundff2323 h3 w' w)
  g.Move(A_ScreenWidth, A_ScreenHeight - h), g.Show('NA')

  curTime := A_TickCount
  duration := 1500, dist := A_ScreenWidth - w
  t := 200, a := 10000, _checked := false

  SetTimer fn, 1

  fn(*) {
    g.Move(Floor(Lerp(A_ScreenWidth, dist, bounceOut((A_TickCount - curTime) / duration))))
    if A_TickCount - curTime >= duration {
      SetTimer fn, 0
      g.Move(dist)
      loop t {
        if _checked
          break
        p.Move(, , w - (w / t) * A_Index)
        Sleep a / t
      }
      Animation.FadeOut(g, , 50)
      if !_checked
        TrayTip(str, , 0x10)
    }
  }

}

; 测试
; Toast('test', FormatTime(, 'HH:mm:ss'), 5000)
